package com.mj;

import java.util.Map;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.androidquery.callback.BitmapAjaxCallback;
import com.mj.bean.UserInfo;
import com.mj.http.NetCallback;

public class MJApplaction extends Application {
	public static UserInfo userInfo;
	public static NetCallback callback = null;
	public static String url = null;
	public static Map<String, Object> params = null;

	@Override
	public void onCreate() {
		super.onCreate();
		initAQuery();
		initUserInfo();
	}

	// 初始化用户信息
	private void initUserInfo() {
		userInfo = new UserInfo();
		SharedPreferences preferences = getSharedPreferences("user", Context.MODE_PRIVATE);
		userInfo.setToken(preferences.getString("token", ""));
		userInfo.setUserkey(preferences.getString("userkey", ""));
		userInfo.setId(preferences.getString("id", ""));
		userInfo.setAvatar(preferences.getString("avatar", ""));
		userInfo.setNickname(preferences.getString("nickname", ""));
	}

	/***
	 * 清空登录之后的回调信息
	 */
	public static void cleanLoginInfo() {
		callback = null;
		url = null;
		params = null;
	}

	// 初始化AQuery
	private void initAQuery() {
		// set the max number of concurrent network connections, default is 4
		// AjaxCallback.setNetworkLimit(8);
		// set the max number of icons (image width <= 50) to be cached in
		// memory, default is 20
		// BitmapAjaxCallback.setIconCacheLimit(20);
		// set the max number of images (image width > 50) to be cached in
		// memory, default is 20
		// BitmapAjaxCallback.setCacheLimit(40);
		// set the max size of an image to be cached in memory, default is 1600
		// pixels (ie. 400x400)
		BitmapAjaxCallback.setPixelLimit(400 * 400);
		BitmapAjaxCallback.setDelayWrite(true);
		// set the max size of the memory cache, default is 1M pixels (4MB)
		// BitmapAjaxCallback.setMaxPixelLimit(2000000);
	}

	@Override
	public void onLowMemory() {
		BitmapAjaxCallback.clearCache();
	}
}
